/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork;
import java.io.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
/**
 *
 * @author ThanhTri
 */
public final class Level {
    private static Level instance = null;
    public static Level get (){
         if( instance == null){
             instance =  new Level();
         }
         return instance;
    }
    private int m_TotalLevel;
    private int m_CurLevel;
    private int m_UnlockLevel;
    boolean m_Check;
    private Level (){
        m_TotalLevel = 21;
        m_CurLevel = 1;
        m_Check = true;
        m_UnlockLevel = loadData();
    }
    public boolean setCurLevel (int _Level){
        if(_Level<=m_UnlockLevel ){
            m_CurLevel = _Level;
            return true;
        }
        return false;
    }
    public void setUnlockLevel (int _Level){
        if(_Level<= m_TotalLevel){
            m_UnlockLevel = _Level;
        }
    }
    public int getCurLevel (){
        return m_CurLevel;
    }
    public int getUnlockLevel (){
        return m_UnlockLevel;
    }
    public int getTotalLevel (){
        return m_TotalLevel;
    }
    public boolean  NextLevel (){
        if (UnlockNextLevel()){
            m_CurLevel++;
            return true;
        }
        if(setCurLevel(getCurLevel()+1)){
            return true;
        }
        return false;
           
    }
    public boolean UnlockNextLevel (){
        if(m_CurLevel==m_UnlockLevel &&
                m_UnlockLevel<m_TotalLevel){
            m_UnlockLevel++;
            return true;
        }
        return false;
    }
    
    public void saveData() {
        try {
            RecordStore options = RecordStore.openRecordStore("data", true);
            byte[] data = saveOptions();

            if (options.getNumRecords() != 0){
                options.setRecord(1, data, 0, data.length);
            }else {
                options.addRecord(data, 0, data.length);
            }

            options.closeRecordStore();
        } catch (RecordStoreException ex) {
        }
    }

    public byte[] saveOptions() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        try {
        dos.writeBoolean(m_Check);
        dos.writeInt(getUnlockLevel());
        dos.flush();
        } catch (IOException ex) {
        }
        return baos.toByteArray();
    }
    
    public int loadData() {
        try {
            RecordStore options = RecordStore.openRecordStore("data", true);
            if (options.getNumRecords() != 0) {
                loadOptions(options.getRecord(1));
            }else{
                return 1;
            }
            options.closeRecordStore();
            
        } catch (RecordStoreException ex) {
        }
        return getUnlockLevel();
    }
    
    public void loadOptions(byte[] data) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        
        try {
        m_Check = dis.readBoolean();
        setUnlockLevel(dis.readInt());
        dis.close();
        } catch (IOException ex) {

        }
    }
}
    

        
